import React from 'react'
// ===================================================================== Temp
import SubmitForm from '#cpt/temp/SubmitForm'
import Controls from '#cpt/temp/Controls'
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const Button 	= $Lazy.load(()=>import('@antd/button'))
// ===================================================================== Lazy
const width = '80%'
const tempType = [{ name:'表格', value: '0' },{ name:'文档', value: '1' },{ name:'文本', value: '2' }]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		// 提交表单列表
		formData : [
			{ label: '模板名称',			name: 'temp_name', 			width, p:'--', title:'模板信息' },
			{ type:'cpt', title:'版本列表' },
		],
	}
	
	componentDidMount(){
		let { uuid, temp_name } = $fn.query()
		this.query = { temp_uuid:uuid }
		this.temp_name = $fn.decode(temp_name)
		this.fetch()
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'conf-import-temp/version')
	
	// 表格
	cols = [
		{ title: '版本号', 		dataIndex: 'v_num', 		width:150 },
		{ title: '上传时间', 	dataIndex: 'create_at', 	width:150 },
		{ title: '文档类型', 	dataIndex: 'temp_type', 	width:100, render:text=>$fn.getState(tempType,text) },
		{ title: '描述', 		dataIndex: 'remark', 		width:100 },
		{ title: '是否默认', 	dataIndex: 'is_default', 	width:100, render: text => +text === 1 ? '是' : '否' },
		TableControls.controls.call(this,{ width: 100, align:'center', title:'默认模板' }),
		{ title:'操作', width:80, align:'center', render:(text, { temp_url })=>{
			return <Button ghost disabled={!temp_url} onClick={()=> window.location.href=temp_url} label='下载' />
		} }
	]
	// 表格操作按钮
	table_controls = (text, { is_default, uuid }) => {
		return [
			TableControls.Switch.call(this, 'conf-import-temp/default', { uuid }, { value:+is_default, disValue:true }),
		]
	}
	// 操作按钮
	controls = () => [
		Controls.back.call(this)
	]
	
	render(){
		return (
			<Page title='版本管理' controls={this.controls()}>
				<div className='plr10'>{ SubmitForm.call(this,{ labelWidth:'s11', mb:5, msg:'保存', api:'', readOnly: true , onForm:  ref => ref.setValue({temp_name:this.temp_name}) }) }</div>
				{/* 表格 */}
				{ Table.call(this) }
			</Page>
		)
	}
}

export default Index